#include "ThreadPool.hpp"
#include<cstdio>
#include<ctime>
#include<unistd.h>

class Task 
{
private:
    int x;
    int y;
    char op;
public:
    Task(){}
    Task(int _x,int _y,char _op):x(_x),y(_y),op(_op){}
    void operator()()
    {
        Handler();
    }
    void Handler()
    {
        int res;
        switch(op)
        {
            case '+':
                res=x+y;
                break;
            case '-':
                res=x-y;
                break;
            case '*':
                res=x*y;
                break;
            case '/':
                res=x/y;
                break;
            default:
                break;
        }
        printf("thread:%p done the task: %d %c %d = %d\n",pthread_self(),x,op,y,res);
    }
};

int main()
{
    srand((unsigned)time(nullptr));
    ThreadPool<Task>* tp=new ThreadPool<Task>(NUM);
    tp->InitThreadPool(); 
    char ch[]="+-*/";
    while(true)
    {
        int x=rand()%1000+1;
        int y=rand()%1000+1;
        int op=ch[rand()%4];
        tp->Put(Task(x,y,op));
        sleep(1);
    }
    delete tp;
}
